home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13302 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  72 lines

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: annoying@usa.pipeline.com(Zachary Hartnett)
  3. Newsgroups: comp.lang.c++
  4. Subject: Bit Fields in Structures
  5. Date: 24 Mar 1996 20:54:17 GMT
  6. Organization: Pipeline USA
  7. Message-ID: <4j4cpp$i8o@news1.h1.usa.pipeline.com>
  8. NNTP-Posting-Host: 38.8.60.5
  9. X-PipeUser: annoying
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Zachary Hartnett)
  12. X-Newsreader: Pipeline v3.5.0
  13.  
  14. /* Here's one for all you C++ gurus out there! Why does the 
  15.    following code fragment fail to compile? I've included notes 
  16.    that seem to indicate it should... 
  17.  
  18.    Thanks! (annoying@usa.pipeline.com) 
  19. */ 
  20.  
  21. /* NOTE: 
  22. *  Minimum sizes of fundamental types: 
  23. *     char:    8 bits 
  24. *     short:   16 bits 
  25. *     long:    32 bits 
  26. *  Ref: Bjarne Stroustrup, "The C++ Programming Language", 2d Ed., 
  27. *       pg 50. 
  28. */ 
  29.  
  30. /* NOTE: 
  31. *  Types char, int of all sizes, and enumerations are collectively 
  32. *  called integral types. 
  33. *  Ref: Bjarne Stroustrup, "The C++ Programming Language", 2d Ed., 
  34. *       pg 487. 
  35. */ 
  36.  
  37. /* NOTE: 
  38. *  For a more compact notation, int can be dropped from multiword 
  39. *  combinations without changing the meaning; thus long means long 
  40. *  int and unsigned means unsigned int. 
  41. *  Ref: Bjarne Stroustrup, "The C++ Programming Language", 2d Ed., 
  42. *       pg 49-50. 
  43. */ 
  44.  
  45. struct DateStr 
  46.    long  month : 8;  // WHY WON'T THIS COMPILE? (Borland C++ v4.52) 
  47.    long  day   : 8;  // ERROR: Bit fields must have integral type 
  48.    long  year  : 16; // Only works with type int?! Why? 
  49. }; 
  50.  
  51. /* This is the structure I would LIKE to make: 
  52.    struct DateStr 
  53.    { 
  54.       unsigned long int month : 4;  // 0-15    : Range is 1-12 
  55.       unsigned long int day   : 5;  // 0-31    : Range is 1-31 
  56.       unsigned long int year  : 14; // 0-16383 : Range is 0000-9999 
  57.       unsigned long int epoch : 1;  // 0-1     : Range is 0-1 (BC/AD) 
  58.       unsigned long int       : 8; 
  59.    }; 
  60. */ 
  61.  
  62. #include <iostream.h> 
  63. #include <stdlib.h> 
  64. void main(void) 
  65.    DateStr date; 
  66.  
  67.    cout << "sizeof (date) = " << sizeof (date) << '.'; 
  68.  
  69.